package com.dhl.commonutils.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

import static java.time.ZoneId.SHORT_IDS;

/**
 * 自定义返回JSON 数据格式中日期格式化处理
 * @author java1234 小锋 老师
 *
 */
public class CustomDateTimeSerializerUtils extends JsonSerializer<Date>{

	@Override
	public void serialize(Date date, JsonGenerator gen, SerializerProvider serializers)
			throws IOException {
		DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
////		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		dateFormat.withZone(ZoneId.of("CTT"));
////		sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		gen.writeString(dateFormat.format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault())));
	}
}
